package cn.sjxy.apiClient;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.mapper.Mp_BookInfo;
import cn.sjxy.mapper.Mp_BookType;
import cn.sjxy.model.Md_BookInfo;
import cn.sjxy.model.Md_BookType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 客户端 - 图书信息API
 */
@RestController
@Api(tags = "客户端-图书信息")
public class Ci_BookInfo {

    @Autowired
    private Mp_BookInfo mp_bookInfo;

    @Autowired
    private Mp_BookType mp_bookType;

    /**
     * 获取图书列表（分页）
     *
     * @param bookName  图书名称（模糊查询）
     * @param typeCode  图书类型
     * @param isSell    是否在售
     * @param pageIndex 页码（默认1）
     * @param pageSize  每页数量（默认10）
     * @return
     */
    @GetMapping(value = "/bookInfo/getTable")
    @ApiOperation("获取图书列表")
    public AjaxResult getTable(
            @RequestParam(required = false) String bookName,
            @RequestParam(required = false) String typeCode,
            @RequestParam(required = false) String isSell,
            @RequestParam(defaultValue = "1") Integer pageIndex,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        
        // 构建查询条件
        QueryWrapper<Md_BookInfo> wrapper = new QueryWrapper<>();
        
        // 图书名称模糊查询
        if (bookName != null && !bookName.trim().isEmpty()) {
            wrapper.like("bookName", bookName);
        }
        
        // 图书类型精确查询
        if (typeCode != null && !typeCode.trim().isEmpty()) {
            wrapper.eq("typeCode", typeCode);
        }
        
        // 是否在售
        if (isSell != null && !isSell.trim().isEmpty()) {
            wrapper.eq("isSell", isSell);
        }
        
        // 按添加时间倒序（最新的在前）
        wrapper.orderByDesc("addDate");

        // 分页查询
        IPage<Md_BookInfo> page = new Page<>(pageIndex, pageSize);
        page = mp_bookInfo.selectPage(page, wrapper);

        // 返回结果
        AjaxResult result = AjaxResult.success("查询成功");
        result.addTable(page.getRecords());
        result.addPage(page);
        return result;
    }

    /**
     * 根据ID获取图书详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/bookInfo/getById")
    @ApiOperation("获取图书详情")
    public AjaxResult getById(Long id) {
        Md_BookInfo bookInfo = mp_bookInfo.selectById(id);
        AjaxResult result = AjaxResult.success("查询成功");
        result.addKey("data", bookInfo);
        return result;
    }

    /**
     * 获取图书详情（用于图书详情页）
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/bookInfo/getDetail")
    @ApiOperation("获取图书详情（带类型名称）")
    public AjaxResult getDetail(Long id) {
        Md_BookInfo bookInfo = mp_bookInfo.selectById(id);
        
        if (bookInfo != null) {
            // 查询类型名称
            QueryWrapper<Md_BookType> typeWrapper = new QueryWrapper<>();
            typeWrapper.eq("typeCode", bookInfo.getTypeCode());
            Md_BookType bookType = mp_bookType.selectOne(typeWrapper);
            if (bookType != null) {
                bookInfo.setTypeName(bookType.getTypeName());
            }
        }
        
        AjaxResult result = AjaxResult.success("查询成功");
        result.addKey("entry", bookInfo);
        return result;
    }
}

